﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RunUI
{
    public static class ModelStateDictionaryExtensions
    {
        public static Dictionary<string, string> ToErrorString(this ModelStateDictionary modelState)
        {
            var Errors = new Dictionary<string, string>();
            foreach (var model in modelState)
                if (model.Value.Errors.Count > 0)
                {
                    var errors = new StringBuilder();
                    foreach (var error in model.Value.Errors) errors.AppendLine(error.ErrorMessage);
                    Errors.Add(model.Key, errors.ToString());
                }

            //var msg = Errors.Aggregate(string.Empty, (current, item) => current + item.Value + "<br />");
            return Errors;
        }
    }
}
